﻿using phhui;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace PQBlog
{
    public partial class DiaryView : Form
    {
        private string curId;
        private int curIndex=0;
        public DiaryView()
        {
            InitializeComponent();
        }

        private void DiaryView_Load(object sender, EventArgs e)
        {
            bindList();
            btn_delete.Visible = false;
            btn_submit.Visible = false;
            tb_content.MouseWheel += Tb_content_MouseWheel;
        }

        private void Tb_content_MouseWheel(object sender, MouseEventArgs e)
        {
            curIndex -= e.Delta;
            if (curIndex <= 1) curIndex = 1;
            int num = curIndex / 40;
            int n = Convert.ToInt32(tb_content.Height / tb_content.Font.Size);
            if (n + num >= tb_content.Lines.Length)
            {
                curIndex += e.Delta;
                return;
            }
            string str = "";
            for (int i= num; i <= n+ num; i++) str += i.ToString() + "\n";
            tb_num.Text = str;
        }

        private void bindList()
        {
            lb_list.Items.Clear();
            XmlElement xe = (XmlElement)XmlHelper.self.GetXmlNodeByXpath("db/info.xml", "data//dir");
            string[] list = xe.InnerText.Split(',');
            XmlNodeList xnl = XmlHelper.self.GetXmlNodeListByXpath("db/article/" + list[0] + ".xml", "data//item");
            if (xnl == null) return;
            foreach (XmlNode xn in xnl)
            {
                xe = (XmlElement)xn;
                lb_list.Items.Add(new ListItem("db/article/" + xe.GetAttribute("date") + "//" + xe.GetAttribute("fn") + ".xml", xe.GetAttribute("name")));
            }
        }
        private void bindItem()
        {
            XmlNode xe = (XmlNode)XmlHelper.self.GetXmlNodeByXpath(curId, "data//title");
            if (xe == null)
            {
                MessageBox.Show("文件" + curId + "不存在！");
                return;
            }
            tb_title.Text = xe.InnerText;
            xe = (XmlNode)XmlHelper.self.GetXmlNodeByXpath(curId, "data//tag");
            tb_tag.Text = xe.InnerText;
            xe = (XmlNode)XmlHelper.self.GetXmlNodeByXpath(curId, "data//content");
            tb_content.Text = xe.InnerText;
        }
        private void btn_submit_Click(object sender, EventArgs e)
        {
            if(btn_submit.Text=="编  辑")
            {
                tb_content.ReadOnly = false;
                tb_tag.ReadOnly = false;
                tb_title.ReadOnly = false;
                btn_submit.Text = "更  新";
            }
            else
            {
                if (!CharCheck.unNull(tb_title.Text))MessageBox.Show("标题不能为空！");
                else if(!CharCheck.unNull(tb_content.Text)) MessageBox.Show("内容不能为空！");
                else
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("title", tb_title.Text);
                    ht.Add("tag", tb_tag.Text);
                    ht.Add("content", tb_content.Text);
                    if(XmlHelper.self.UpdateNode(curId, "data", null, ht))
                    {
                        tb_content.ReadOnly = true;
                        tb_tag.ReadOnly = true;
                        tb_title.ReadOnly = true;
                        btn_submit.Text = "编  辑";
                        Dictionary<string, string> dir = new Dictionary<string,string>();
                        dir.Add("fn",curId.Substring(curId.LastIndexOf("/")+1).Replace(".xml",""));

                        Dictionary<string, string> newdir = new Dictionary<string, string>();
                        newdir.Add("name", tb_title.Text);
                        XmlHelper.self.UpdateNode("db/article/dir.xml", "data", dir, newdir, null);

                        MessageBox.Show("更新成功！");
                        bindList();
                        bindItem();
                    }
                }
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定删除该文章？", "对话框标题", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                try {
                    File.Delete(curId);
                    MessageBox.Show("成功删除！");
                    btn_delete.Visible = false;
                    tb_content.Text = "";
                    tb_tag.Text = "";
                    tb_title.Text = "";
                    Dictionary<string, string> dir = new Dictionary<string, string>();
                    dir.Add("fn", curId.Substring(curId.LastIndexOf("/") + 1).Replace(".xml", ""));
                    XmlHelper.self.DeleteNodes("db/article/dir.xml", "data", dir);
                    bindList();
                    curId = "";
                }
                catch(Exception err)
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void lb_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem li = (ListItem)lb_list.SelectedItem;
            if (li == null) return;
            curId = li.ID;
            tb_content.ReadOnly = true;
            tb_tag.ReadOnly = true;
            tb_title.ReadOnly = true;
            btn_submit.Visible = true;
            btn_delete.Visible = true;
            bindItem();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            this.Hide();
            WinMgr.self.showAddDiary();
        }

        private void DiaryView_Activated(object sender, EventArgs e)
        {
            bindList();
        }

        private void DiaryView_FormClosed(object sender, FormClosedEventArgs e)
        {
            WinMgr.self.showMain();
        }

        private void tb_content_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
